﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;

namespace jQueryDotNetLib.Extenders
{
    public enum EffectTypes { none, blind, bounce, clip, drop, explode, fold, highlight, puff, pulsate, scale, shake, size, slide, transfer };

    [ToolboxItem(true)]
    [ToolboxData("<{0}:Effect TriggerSelector=\"\" Selector=\"\" Event=\"\" EffectType=\"\" runat=server />")]
    public class Effect : Control
    {

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TriggerSelector
        {
            get
            {
                if (ViewState["TriggerSelector"] == null) return "";
                return (string)ViewState["TriggerSelector"];
            }
            set
            {
                ViewState["TriggerSelector"] = value;
            }
        }
        string resolvedTriggerSelector = null;
        public string ResolvedTriggerSelector
        {
            get
            {
                if (resolvedTriggerSelector == null)
                    resolvedTriggerSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.TriggerSelector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedTriggerSelector;
            }
        }
        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Selector
        {
            get
            {
                if (ViewState["Selector"] == null) return "";
                return (string)ViewState["Selector"];
            }
            set
            {
                ViewState["Selector"] = value;
            }
        }
        string resolvedSelector = null;
        public string ResolvedSelector
        {
            get
            {
                if (resolvedSelector == null)
                    resolvedSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.Selector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedSelector;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Event
        {
            get
            {
                if (ViewState["Event"] == null) return "click";
                return (string)ViewState["Event"];
            }

            set
            {
                ViewState["Event"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Toggle
        {
            get
            {
                if (ViewState["Toggle"] == null) return false;
                return (bool)ViewState["Toggle"];
            }

            set
            {
                ViewState["Toggle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public EffectTypes EffectType
        {
            get
            {
                if (ViewState["EffectType"] == null) return EffectTypes.none;
                return (EffectTypes)ViewState["EffectType"];
            }

            set
            {
                ViewState["EffectType"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Options
        {
            get
            {
                if (ViewState["Options"] == null) return "";
                return (string)ViewState["Options"];
            }

            set
            {
                ViewState["Options"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Speed
        {
            get
            {
                if (ViewState["Speed"] == null) return "";
                return (string)ViewState["Speed"];
            }

            set
            {
                ViewState["Speed"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Callback
        {
            get
            {
                if (ViewState["Callback"] == null) return "";
                return (string)ViewState["Callback"];
            }

            set
            {
                ViewState["Callback"] = value;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            System.Text.StringBuilder sb = new StringBuilder();

            if (TriggerSelector == "") TriggerSelector = Selector;
            if (Selector == "") Selector = TriggerSelector;
            resolvedSelector = null;
            resolvedTriggerSelector = null;

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("jQuery(\"" + ResolvedTriggerSelector + "\").bind('" + Event + "', function(e) {jQuery(\"" + ResolvedSelector + "\")." + ((Toggle) ? "toggle" : "effect") + "(");

            string spd = "'" + Speed + "'";
            int speedint = 0;
            if (int.TryParse(Speed, out speedint))
                spd = Speed;

            if (spd == "''") spd = "'normal'";

            sb.Append("'" + EffectType.ToString() + "'," + ((Options != "") ? "{" + Options + "}" : "") + ", " + spd + ((Callback != "") ? ", " + Callback : ""));

            sb.Append(")});");
            sb.Append("</script>");
            writer.Write(sb.ToString());

        }

    }
}